﻿using System;
using System.Windows.Forms;

namespace VisualPureBasicIDE.CoreGUI.SolutionManager
{
    public class SolutionTreeNode : TreeNode
    {
        #region Members
        //Node type: Solution, Project, File, Folder
        public SolutionTreeNodeTypes Type; //TODO: Change variable to property

        //Full path
        public string Path; //TODO: Change variable to property

        //Is included in project
        public bool IsIncluded = true; //TODO: Change variable to property
        #endregion

        #region Constructors
        public SolutionTreeNode()
        {
        }

        public SolutionTreeNode(string text, SolutionTreeNodeTypes type, string path, TreeIcons icon)
        {
            Text = text;
            Type = type;
            Path = path;
            ToolTipText = text;
            ImageIndex = (int)icon;
            SelectedImageIndex = (int)icon;
        }

        public SolutionTreeNode(string text, SolutionTreeNodeTypes type, string path)
        {
            Text = text;
            Type = type;
            Path = path;
            ToolTipText = text;

            #region Icon
            switch (Type)
            {
                case SolutionTreeNodeTypes.Solution:
                    ImageIndex = (int)TreeIcons.Solution;
                    SelectedImageIndex = (int)TreeIcons.Solution;
                    break;

                case SolutionTreeNodeTypes.Project:
                    ImageIndex = (int)TreeIcons.Project;
                    SelectedImageIndex = (int)TreeIcons.Project;
                    break;

                case SolutionTreeNodeTypes.VirtualFolder:
                    ImageIndex = (int)TreeIcons.FolderVirtualClosed;
                    SelectedImageIndex = (int)TreeIcons.FolderVirtualClosed;
                    break;

                case SolutionTreeNodeTypes.Folder:
                    ImageIndex = (int)TreeIcons.FolderClosed;
                    SelectedImageIndex = (int)TreeIcons.FolderClosed;
                    break;

                case SolutionTreeNodeTypes.File:
                    var index = GetIconIndex(Path);
                    ImageIndex = index;
                    SelectedImageIndex = index;
                    break;
            }
            #endregion
        }
        #endregion

        #region Properties
        public TreeIcons Icon
        {
            get
            {
                var obj = Enum.Parse(typeof(TreeIcons), ImageIndex.ToString());
                if (obj != null && obj is TreeIcons)
                {
                    return (TreeIcons)obj;
                }

                return TreeIcons.Unknown;
            }
            set
            {
                ImageIndex = (int)value;
                SelectedImageIndex = (int)value;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Get index of icon from file extension
        /// </summary>
        /// <param name="filename">Filename that contains extension</param>
        /// <returns>Icon index from TreeIcons enum</returns>
        public int GetIconIndex(string filename)
        {
            var index = (int)TreeIcons.File;
            if (filename != null)
            {
                var ext = System.IO.Path.GetExtension(filename).ToLower();
                switch (ext)
                {
                        //TODO: Define icons by extension
                    case ".cs": index = (int)TreeIcons.FileCs; break;
                    case ".pb": index = (int)TreeIcons.FilePb; break;
                    case ".dll": index = (int)TreeIcons.FileDll; break;
                    case ".resx": index = (int)TreeIcons.FileResx; break;
                }
            }
            return index;
        }
        #endregion
    }
}